package com.chu.utils;

import com.chu.entity.Employee;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

//定义导出数据的代码
public class ExcelExport {

    //先创建一个Excel文件
    static HSSFWorkbook workbook ;
    //创建文件中的sheet表 参数是sheet名称
    static HSSFSheet sheet;
    //创建sheet中的行列信息

    //创建第一行
    public static void createFilst(String headName){
        workbook = new HSSFWorkbook();
        sheet =  workbook.createSheet(headName);
        HSSFRow row = sheet.createRow(0);//第一行
        String[] params = {"账号","姓名","所属部门","角色"};
        for(int i=0;i<params.length;i++){
            HSSFCell cell = row.createCell(i);//创建列
            cell.setCellValue(params[i]);
        }
    }
    //创建第一行以外的其他行 参数表示被导出的数据集合
    public static void createOther(List<Employee> list){
        for(int i=0;i<list.size();i++){
            HSSFRow row = sheet.createRow(i + 1);
            Employee employee = list.get(i);
            HSSFCell cella = row.createCell(0);//第一列
            cella.setCellValue(employee.getEmployeenum());

            HSSFCell cellb = row.createCell(1);//第二列
            cellb.setCellValue(employee.getUsername());

            HSSFCell cellc = row.createCell(2);//第三列
            cellc.setCellValue(employee.getDept().getDeptname());

            HSSFCell celld = row.createCell(3);//第四列
            celld.setCellValue(employee.getRole().getRname());

        }
    }
    //设置导出信息
    public static void exports(OutputStream outputStream) throws IOException {
        //设置输出的方式是以表格的方式输出
        sheet.setGridsPrinted(true);
        workbook.write(outputStream);

    }

}
